package com.haierp.service.impl;

import java.io.InputStream;

import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.haierp.service.IUploadFileService;


@Service("uploadFileService")
public class UploadFileServiceImpl implements IUploadFileService {
	// endpoint以杭州为例，其它region请按实际情况填写
	String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; 
	// accessKey请登录https://ak-console.aliyun.com/#/查看
	/*String accessKeyId = "LTAISRg0cXghto8b";
	String accessKeySecret = "tRqkBoUk6H8mfY38movbVIyWHyv37f";
	String bucketName = "haierp";*/
	String accessKeyId = "LTAIVI0EwNcDNmUe";
	String accessKeySecret = "hFBvxmCDoX5XOZALYENHjl4Q7I8Hsl";
	String bucketName = "iruhua";
	String bucketNameFile = "iruhua-file";
	String bucketNameVideo = "iruhua-item";
	String bucketNameCard = "iruhua-card";
	private OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
	
	private String urlhead = "http://" + bucketName + ".oss-cn-hangzhou.aliyuncs.com/";
	private String urlheadFile = "http://" + bucketNameFile + ".oss-cn-hangzhou.aliyuncs.com/";
	private String urlheadVideo = "http://" + bucketNameVideo + ".oss-cn-hangzhou.aliyuncs.com/";
	private String urlheadCard = "http://" + bucketNameCard + ".oss-cn-hangzhou.aliyuncs.com/";
	
	@Override
	public String uploadImg(InputStream inputStream, String picKey) {
		ObjectMetadata objectMeta = new ObjectMetadata();
//         objectMeta.setContentLength(inputStream.);
		objectMeta.setContentType("image/jpeg");
		ossClient.putObject(bucketName, picKey, inputStream,objectMeta);
		return urlhead+picKey;
	}

	@Override
	public String uploadFile(InputStream inputStream, String picKey) {
		ObjectMetadata objectMeta = new ObjectMetadata();
//         objectMeta.setContentLength(inputStream.);
		//objectMeta.setContentType("image/jpeg");
		objectMeta.setContentType("application/octet-stream");
		ossClient.putObject(bucketNameFile, picKey, inputStream,objectMeta);
		return urlheadFile+picKey;
	}

	@Override
	public String uploadVideo(InputStream inputStream, String picKey) {
		ObjectMetadata objectMeta = new ObjectMetadata();
//         objectMeta.setContentLength(inputStream.);
		//objectMeta.setContentType("image/jpeg");
		objectMeta.setContentType("video/mpeg4");
		ossClient.putObject(bucketNameVideo, picKey, inputStream,objectMeta);
		return urlheadVideo+picKey;
	}

	@Override
	public Boolean fileDelete(String src) {
		String key = src.substring(src.lastIndexOf("/")+1);
		String delbucketName = src.substring(src.indexOf("//")+2,src.indexOf("."));
		ossClient.deleteObject(delbucketName, key);
		return true;
	}

	@Override
	public OSSObject getOSSObject(String picKey) {
		OSSObject object = null;
		try {
			object = ossClient.getObject(bucketName, picKey);
		} catch(Exception e) {
			object = ossClient.getObject("iruhua-card", picKey);
		}
		return object;
	}
	
	@Override
	public String uploadImgCard(InputStream inputStream, String picKey) {
		ObjectMetadata objectMeta = new ObjectMetadata();
		objectMeta.setContentType("image/jpeg");
		ossClient.putObject(bucketNameCard, picKey, inputStream,objectMeta);
		return urlheadCard+picKey;
	}

}
